/* Copyright (c) 2002-2009 Grupo e-Gen.  All rights reserved.
 *
 * ====================================================================
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * either version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 */

package br.com.managerprojectplugin.view;

import java.util.ArrayList;
import java.util.Collections;

import managerprojectsplugin.handlers.OpenEditUseCaseEditorHandler;
import managerprojectsplugin.handlers.OpenUseCaseNewWizardHandler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import br.com.managerprojectplugin.model.Usecase;

/**
 * Class: UseCase
 *   
 * @author Grupo e-Gen 
 * @version 1.0 (07/07/2012)
 * @since 1.0 (14/07/2012)
 */
public class UseCaseView extends ViewPart {

	public static final String USECASEVIEW_ID = "UseCaseView"; //$NON-NLS-1$
	private ArrayList<Usecase> ucFather = new ArrayList<Usecase>();

	private TreeViewer treeViewer;

	@Override
	public void createPartControl(Composite parent) {
		ucFather.add(new Usecase("First", "plugin", "new", "20/08/2012", "24/08/2012", "Em desenvolvimento", null)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$




		treeViewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
		treeViewer.setContentProvider(new UseCaseContentProvider());
		treeViewer.setLabelProvider(new UseCaseLabelProvider());
		treeViewer.setInput(ucFather.toArray());
		treeViewer.setAutoExpandLevel(2);
		treeViewer.getTree().setLinesVisible(true);
		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
		rowLayout.fill = true;
		rowLayout.spacing = 6;
		Composite compositeButtons= new Composite(parent, SWT.NONE);
		compositeButtons.setLayout(rowLayout);
		GridData gridData = new GridData();
		gridData.verticalAlignment = SWT.BEGINNING;
		compositeButtons.setLayoutData(gridData);

		Button newButton = new Button(compositeButtons, SWT.PUSH);
		newButton.setText("+"); //$NON-NLS-1$
		newButton.setToolTipText(Messages.UseCaseView_addnewusecasechildrenselection);
		newButton.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent event) {
				ITreeSelection treeSelection = (ITreeSelection) treeViewer.getSelection();
				if(treeSelection.getFirstElement() instanceof Usecase){
					Usecase us = (Usecase) treeSelection.getFirstElement();
					OpenUseCaseNewWizardHandler handler = new OpenUseCaseNewWizardHandler();
					ExecutionEvent ev = new ExecutionEvent(null, Collections.EMPTY_MAP, us, null);
					try {
						handler.execute(ev);
						us = handler.getUseCase();
						treeViewer.refresh();
					} catch (ExecutionException e) {
						System.out.println(Messages.UseCaseView_errorwizard);
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		Button delButton = new Button(compositeButtons, SWT.PUSH);
		delButton.setText("x"); //$NON-NLS-1$
		delButton.setToolTipText(Messages.UseCaseView_deleteusecase);
		delButton.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent event) {
				ITreeSelection treeSelection = (ITreeSelection) treeViewer.getSelection();
				if(treeSelection.getFirstElement() instanceof Usecase){
					Usecase us = (Usecase) treeSelection.getFirstElement();
					Usecase parentCase = us.getUseCaseParent();
					if(parentCase != null){
						parentCase.getUseCaseChildren().remove(us);
						treeViewer.refresh();
					}
					else{
						MessageDialog md = new MessageDialog(treeViewer.getControl().getShell(), Messages.UseCaseView_error,null, Messages.UseCaseView_cannotdeletefirstnode, MessageDialog.ERROR, new String[]{Messages.UseCaseView_close}, 0);
						md.open();
					}
				}
				
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});

		Button editButton = new Button(compositeButtons, SWT.PUSH);
		editButton.setText(Messages.UseCaseView_edit);
		editButton.setToolTipText(Messages.UseCaseView_editselectedusecase);



		treeViewer.addDoubleClickListener(new IDoubleClickListener() {

			@Override
			public void doubleClick(DoubleClickEvent event) {
				ITreeSelection treeSelection = (ITreeSelection) event.getSelection();
				if(treeSelection.getFirstElement() instanceof Usecase){
					Usecase us = (Usecase) treeSelection.getFirstElement();
					OpenEditUseCaseEditorHandler handler = new OpenEditUseCaseEditorHandler();
					ExecutionEvent ev = new ExecutionEvent(null, Collections.EMPTY_MAP, us, null);
					try {
						handler.execute(ev);
						//						us = handler.getUseCase();
						treeViewer.refresh();
					} catch (ExecutionException e) {
						System.out.println(Messages.UseCaseView_errorwizard);
					}
				}

			}
		});
	}




	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}

}
